package test5;

import java.util.HashMap;

/**
 * 窗口固定和不固定的都可以这么用
 *
 * @Author Jun jie Yi
 * 2022/5/7 22:18
 */
public class Demo3 {
    public static void main(String[] args) {
        String str = "abcabcbb";
        System.out.println(getLongNum(str));
    }

    public static int getLongNum(String str) {
        HashMap<Character, Integer> window = new HashMap<>();
        int left = 0, right = 0;
        int res = 0;
        while (right < str.length()) {
            char c = str.charAt(right);
            window.put(c, window.getOrDefault(c, 0) + 1);
            right++;
            //有重复 c 和 d 是重复的
            while (window.get(c) > 1) {//有重复
                char d = str.charAt(left);
                left++;
                window.put(d, window.get(d) - 1);
                res = Math.max(res, right - left);
            }
        }
        return res;
    }
}
